from flask import Blueprint, request, jsonify

from app.routes.user import user_bp
from app.services.property_service import get_property_byIdname
from app.services.repairRequest_service import add_repairRequest, get_repairRequest
from app.services.user_service import create_user, get_user_by_username, get_all_users, update_my_user
from app.utils.jwt_utils import generate_token
from app.utils.jwt_utils import login_required
repairs_bp = Blueprint("repairs", __name__)

# 添加维修请求
@user_bp.route("/repairRequests", methods=["POST"])
@login_required
def add_repair_request():
    print(request.json)
    data = request.json
    id = data.get("id")
    username = data.get("username")
    propertyid = data.get("propertyid")

    usernameowner = get_property_byIdname(propertyid).username
    description = data.get("description")
    phone = data.get("phone")


    add_repairRequest(id,username, propertyid,description,phone,usernameowner)

    return jsonify({"code": 201, "msg": "维修任务条目创建成功"}), 201

# 添加维修请求
@user_bp.route("/repairRequests", methods=["GET"])
@login_required
def get_repair_request():

    print('aaa')
    username = request.args.get('username')



    print(username)
    repairRequests = get_repairRequest(username)
    repairRequest_list = []
    for RErequest in repairRequests:
        RErequest_data = {
            "id": RErequest.id,
            "username": RErequest.username,
            "propertyid": RErequest.propertyid,
            "description": RErequest.description,
            "phone": RErequest.phone,
            "usernameowner": RErequest.usernameowner,

        }
        repairRequest_list.append(RErequest_data)
    print(repairRequest_list)
    return jsonify({"code": 200, "msg": "维修任务条目查找成功", "data": repairRequest_list}), 201



